package com.cskaoyan.service.promote;

import com.cskaoyan.bean.promote.GoodsByTopic;
import com.cskaoyan.bean.promote.Topic;
import com.cskaoyan.bean.promote.TopicExample;
import com.cskaoyan.bean.promote.base.PromotePageData;
import com.cskaoyan.bean.promote.base.PromoteParam;
import com.cskaoyan.bean.promote.bo.BatchDeleteTopicBO;
import com.cskaoyan.bean.promote.bo.TopicListSearchBO;
import com.cskaoyan.bean.promote.exception.AddTopicException;
import com.cskaoyan.bean.promote.exception.DeleteTopicException;
import com.cskaoyan.bean.promote.exception.EchoUpdateTopicException;
import com.cskaoyan.bean.promote.exception.UpdateTopicException;
import com.cskaoyan.bean.promote.vo.EchoUpdateTopicVO;
import com.cskaoyan.mapper.promote.GoodsByTopicMapper;
import com.cskaoyan.mapper.promote.TopicMapper;
import com.github.pagehelper.PageHelper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liXiaoFei
 * @date 2021/10/18 17:10
 */
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    GoodsByTopicMapper goodsByTopicMapper;

    @Override
    public PromotePageData queryTopicList(PromoteParam promoteParam, TopicListSearchBO topicListSearchBO) {
        TopicExample example = new TopicExample();
        TopicExample.Criteria criteria = example.createCriteria();
        if (topicListSearchBO.getTitle() != null && !"".equals(topicListSearchBO.getTitle())){
            criteria.andTitleLike("%" + topicListSearchBO.getTitle() + "%");
        }
        if (topicListSearchBO.getSubtitle() != null && !"".equals(topicListSearchBO.getSubtitle())){
            criteria.andSubtitleLike("%" + topicListSearchBO.getSubtitle() + "%");
        }
        example.setOrderByClause(promoteParam.getSort() + " " + promoteParam.getOrder());
        List<Topic> topics = topicMapper.selectByExample(example);

        return PromotePageData.ok(topics);
    }

    @Override
    public Topic addTopic(Topic topic) throws AddTopicException {
        int code = topicMapper.insertSelective(topic);
        if (code == 0) throw new AddTopicException();
        return topic;
    }

    @Override
    public void deleteTopic(Topic topic) throws DeleteTopicException {
        int code = topicMapper.updateByPrimaryKeySelective(topic);
        if (code == 0) throw new DeleteTopicException();
    }

    @Override
    public void batchDeleteTopic(BatchDeleteTopicBO batchDeleteTopicBO) throws DeleteTopicException {
        List<Integer> ids = batchDeleteTopicBO.getIds();
        for (Integer id : ids) {
            Topic topic = new Topic();
            topic.setId(id);
            topic.setDeleted(true);
            int code = topicMapper.updateByPrimaryKeySelective(topic);
            if (code == 0) throw new DeleteTopicException();
        }
    }

    @Override
    public EchoUpdateTopicVO echoUpdateTopic(Integer id) throws EchoUpdateTopicException {
        Topic topic = topicMapper.selectByPrimaryKey(id);
        if (topic == null) throw new EchoUpdateTopicException();
        List<GoodsByTopic> goodsByTopics = new ArrayList<>();
        for (Integer goodId : topic.getGoods()) {
            GoodsByTopic goodsByTopic = goodsByTopicMapper.selectByPrimaryKey(goodId);
            goodsByTopics.add(goodsByTopic);
        }
        return new EchoUpdateTopicVO(goodsByTopics,topic);
    }

    @Override
    public Topic updateTopic(Topic topic) throws UpdateTopicException {
        topic.setUpdateTime(new Date(System.currentTimeMillis()));
        int code = topicMapper.updateByPrimaryKeySelective(topic);
        if (code == 0) throw new UpdateTopicException();
        return topic;
    }


}
